import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// 缓冲区类
class Buffer {
    private static final int MAX_SIZE = 5;
    private int[] buffer = new int[MAX_SIZE];
    private int count = 0;
    private int in = 0;
    private int out = 0;
    private final Lock lock = new ReentrantLock();
    private final Condition notFull = lock.newCondition();
    private final Condition notEmpty = lock.newCondition();

    // 生产方法
    public void produce(int item) {
        lock.lock();
        try {
            // 缓冲区满则等待
            while (count == MAX_SIZE) {
                notFull.await();
            }
            buffer[in] = item;
            in = (in + 1) % MAX_SIZE;
            count++;
            System.out.println(Thread.currentThread().getName() + " 生产了 " + item + "，当前缓冲区大小: " + count);
            // 通知消费者缓冲区非空
            notEmpty.signal();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock.unlock();
        }
    }

    // 消费方法
    public int consume() {
        lock.lock();
        try {
            // 缓冲区空则等待
            while (count == 0) {
                notEmpty.await();
            }
            int item = buffer[out];
            out = (out + 1) % MAX_SIZE;
            count--;
            System.out.println(Thread.currentThread().getName() + " 消费了 " + item + "，当前缓冲区大小: " + count);
            // 通知生产者缓冲区非满
            notFull.signal();
            return item;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return -1;
        } finally {
            lock.unlock();
        }
    }
}

// 生产者类
class Producer implements Runnable {
    private Buffer buffer;
    private int id;

    public Producer(Buffer buffer, int id) {
        this.buffer = buffer;
        this.id = id;
    }

    @Override
    public void run() {
        while (true) {
            try {
                int item = (int) (Math.random() * 100);
                buffer.produce(item);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

// 消费者类
class Consumer implements Runnable {
    private Buffer buffer;
    private int id;

    public Consumer(Buffer buffer, int id) {
        this.buffer = buffer;
        this.id = id;
    }

    @Override
    public void run() {
        while (true) {
            try {
                buffer.consume();
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

public class ProducerConsumer {
    public static void main(String[] args) {
        Buffer buffer = new Buffer();

        // 创建生产者线程
        Thread producer1 = new Thread(new Producer(buffer, 1), "生产者 1");
        Thread producer2 = new Thread(new Producer(buffer, 2), "生产者 2");

        // 创建消费者线程
        Thread consumer1 = new Thread(new Consumer(buffer, 1), "消费者 1");
        Thread consumer2 = new Thread(new Consumer(buffer, 2), "消费者 2");

        // 启动线程
        producer1.start();
        producer2.start();
        consumer1.start();
        consumer2.start();
    }
}    